export enum AccessEnum {
    ADMIN = 'admin',
    USER = 'user',
    NOT_LOGIN = 'not_login'
  }
  
export const ACCESS_TOKEN_KEY = 'ACCESS_TOKEN'
  
/**
 * 判断用户是否具有某种权限 
 * @param loginUser 登录用户信息
 * @param needAccess 需要的权限
 * @returns 是否具有权限
 */
export const hasAccess = (loginUser: any, needAccess: string = AccessEnum.NOT_LOGIN) => {
    const loginUserAccess = loginUser?.userRole ?? AccessEnum.NOT_LOGIN
    // console.log('hasAccess', loginUserAccess, needAccess);
    if (loginUserAccess === needAccess) {
        return true
    }
    // 只需要登录就可以
    if (needAccess == AccessEnum.USER) {
        if (loginUserAccess == AccessEnum.NOT_LOGIN) {
            return true
        }
    }
    // 管理员
    if (needAccess == AccessEnum.ADMIN) {
        console.log('hasAccess', loginUserAccess, needAccess);
        if (loginUserAccess == AccessEnum.ADMIN) {
            console.log('hasAccess', loginUserAccess, needAccess);
            
            return true
        }
    }
    return false
}

export const setToken = (token: string) => {
    localStorage.setItem(ACCESS_TOKEN_KEY, token)
}

export const getToken = () => {
    return localStorage.getItem(ACCESS_TOKEN_KEY)
}

/**
 * 清除token
 */
export const removeToken = () => {
    localStorage.removeItem(ACCESS_TOKEN_KEY)
}
